package com.wang.myblog.service.impl;

import com.wang.myblog.common.PageResponse;
import com.wang.myblog.entity.Comment;
import com.wang.myblog.mapper.ArticleMapper;
import com.wang.myblog.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements com.wang.myblog.service.CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean insert(Comment comment) {
        if (commentMapper.insert(comment) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
        if (commentMapper.deleteById(id) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByReplyId(Integer id) {
        if (commentMapper.deleteByReplyId(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByArticleId(Integer id) {
        if (commentMapper.deleteByArticleId(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Comment queryById(Integer id) {
        Comment comment = commentMapper.queryById(id);
        comment.setTitle(articleMapper.queryById(comment.getArticleId()).getTitle());
        List<Comment> list = commentMapper.queryReplyComment(comment.getId());
        comment.setReply(list);
        return comment;
    }

    @Override
    public List<Comment> queryAll(Comment comment) {
        List<Comment> comments = commentMapper.queryAll(comment);
        // 获取所有回复
        comments.forEach(temp -> {
            Comment search = new Comment();
            search.setReplyId(temp.getId());
            List<Comment> list = commentMapper.queryAll(search);
            temp.setReply(list);
        });
        return comments;
    }

    @Override
    public boolean update(Comment comment) {
        if (commentMapper.update(comment) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<Comment> queryByArticleIdAndType(Integer aId, Integer type) {
        List<Comment> comments = null;
        comments = commentMapper.queryByArticleIdAndType(aId, type);
        comments.forEach(comment -> {
            List<Comment> list = commentMapper.queryReplyComment(comment.getId());
            comment.setReply(list);
            comment.setTitle(articleMapper.queryById(comment.getArticleId()).getTitle());
        });
        return comments;
    }

    @Override
    public List<Comment> queryUnRead(Integer start, Integer limit) {
        List<Comment> comments = null;
        comments = commentMapper.queryUnRead(start,limit);
        comments.forEach(comment -> {
            List<Comment> list = commentMapper.queryReplyComment(comment.getId());
            comment.setReply(list);
            comment.setTitle(articleMapper.queryById(comment.getArticleId()).getTitle());
        });
        return comments;
    }


    @Override
    public Integer getCount() {
        return commentMapper.getCount();
    }

    @Override
    public Integer getUnReadCount() {
        return commentMapper.getUnReadCount();
    }

    @Override
    public PageResponse queryByType(Integer draw, Integer start, Integer limit, Integer type, String searchMsg) {
        List<Comment> comments=commentMapper.queryByType(start, limit, type, searchMsg);
        comments.forEach(comment -> {
            Comment search = new Comment();
            search.setReplyId(comment.getId());
            List<Comment> list = commentMapper.queryAll(search);
            comment.setReply(list);
            comment.setTitle(articleMapper.queryById(comment.getArticleId()).getTitle());
        });
        Integer total=commentMapper.getCountByType(type,searchMsg);
        return new PageResponse(draw,total,total,comments);
    }

    @Override
    public Integer getCountByType(Integer type, String searchMsg) {
        return commentMapper.getCountByType(type, searchMsg);
    }

}
